package com.example.rabbitmq.producer.delay.service;/**
 * ClassName: DelaySender <br/> Description: <br/> date: 2022/1/24 15:49<br/>
 *
 * @author chenzhian<br />
 * @version
 */

import static com.example.common.config.RabbitMQConfig.DELAYED_EXCHANGE;
import static com.example.common.config.RabbitMQConfig.DELAYED_ROUTINGKEY;

import javax.annotation.Resource;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

/**
 * @Author: chenzhian
 * @Date: 2022/1/24 15:49
 * @Description:
 */
@Service
public class DelaySender {

  @Resource
  private RabbitTemplate rabbitTemplate;

  public void send(String message, Integer delayedTime) {
    rabbitTemplate.convertAndSend(DELAYED_EXCHANGE, DELAYED_ROUTINGKEY, message, (msg -> {
      //发送消息 并设置delayedTime
      msg.getMessageProperties().setDelay(delayedTime);
      return msg;
    }));
  }
}